﻿<!DOCTYPE html>
<html lang="en">
<head>
    <title id='Description'>DataTable Cell Editing. Double-click on a cell to begin edit.</title>
	<meta name="description" content="JavaScript dataTable Cell Editing demo" />   	    		
    <link rel="stylesheet" href="../../jqwidgets/styles/jqx.base.css" type="text/css" />
    <script type="text/javascript" src="../../scripts/jquery-1.11.1.min.js"></script>
    <script type="text/javascript" src="../../jqwidgets/jqxcore.js"></script>
    <script type="text/javascript" src="../../jqwidgets/jqxbuttons.js"></script>
    <script type="text/javascript" src="../../jqwidgets/jqxscrollbar.js"></script>
    <script type="text/javascript" src="../../jqwidgets/jqxdatatable.js"></script>
    <script type="text/javascript" src="../../jqwidgets/jqxlistbox.js"></script>
    <script type="text/javascript" src="../../jqwidgets/jqxdropdownlist.js"></script>
    <script type="text/javascript" src="../../jqwidgets/jqxdata.js"></script>
    <script type="text/javascript" src="../../jqwidgets/jqxtooltip.js"></script>
    <script type="text/javascript" src="../../jqwidgets/jqxinput.js"></script>
    <script type="text/javascript" src="../../scripts/demos.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            var orderdetailsurl = "../sampledata/orderdetails.xml";

            var ordersSource =
            {
                dataFields: [
                    { name: 'OrderID', type: 'int' },
                    { name: 'Freight', type: 'float' },
                    { name: 'ShipName', type: 'string' },
                    { name: 'ShipAddress', type: 'string' },
                    { name: 'ShipCity', type: 'string' },
                    { name: 'ShipCountry', type: 'string' },
                    { name: 'ShippedDate', type: 'date' }
                ],
                root: "Orders",
                record: "Order",
                dataType: "xml",
                id: 'OrderID',
                url: orderdetailsurl,
                addRow: function (rowID, rowData, position, commit) {
                    // synchronize with the server - send insert command
                    // call commit with parameter true if the synchronization with the server is successful 
                    // and with parameter false if the synchronization failed.
                    // you can pass additional argument to the commit callback which represents the new ID if it is generated from a DB.
                    commit(true);
                },
                updateRow: function (rowID, rowData, commit) {
                    // synchronize with the server - send update command
                    // call commit with parameter true if the synchronization with the server is successful 
                    // and with parameter false if the synchronization failed.
                    commit(true);
                },
                deleteRow: function (rowID, commit) {
                    // synchronize with the server - send delete command
                    // call commit with parameter true if the synchronization with the server is successful 
                    // and with parameter false if the synchronization failed.
                    commit(true);
                }
            };

            var dataAdapter = new $.jqx.dataAdapter(ordersSource, {
                loadComplete: function () {
                    // data is loaded.
                }
            });
            $("#table").jqxDataTable(
            {
                width: 850,
                source: dataAdapter,
                pageable: true,
                editable: true,
                altRows: true,
                editSettings: { saveOnPageChange: true, saveOnBlur: true, saveOnSelectionChange: true, cancelOnEsc: true, saveOnEnter: true, editSingleCell: true, editOnDoubleClick: true, editOnF2: true },
                pagerButtonsCount: 8,
                columns: [
                  { text: 'Freight', dataField: 'Freight', cellsFormat: 'f', cellsAlign: 'right', align: 'right', width: 250 },
                  {
                      text: 'Ship Country', dataField: 'ShipCountry', width: 250
                  },
                  { text: 'Shipped Date', dataField: 'ShippedDate', cellsAlign: 'right', align: 'right', cellsFormat: 'dd/MM/yyyy' }
                ]
            });

            // Cell Begin Edit
            $("#table").on('cellBeginEdit', function (event) {
                var args = event.args;
                // row key
                var rowKey = args.key;
                // row's index.
                var rowIndex = args.index;
                // row's data.
                var rowData = args.row;
                // row's index in the data source.
                var rowBoundIndex = args.boundIndex;
                // column's data field.
                var columnDataField = args.dataField;
                // column's display field.
                var columnDisplayField = args.displayField;
                // cell's value.
                var value = args.value;
                if (columnDataField == "ShippedDate")
                    value = dataAdapter.formatDate(value, 'dd/MM/yyyy');
                $("#log").html("cellBeginEdit - Row: " + rowIndex + ", Column: " + columnDataField + ", Value: " + value + "<br/>" + $("#log").html());
             });

            // Cell End Edit
            $("#table").on('cellEndEdit', function (event) {
                var args = event.args;
                // row key
                var rowKey = args.key;
                // row's index.
                var rowIndex = args.index;
                // row's data.
                var rowData = args.row;
                // row's index in the data source.
                var rowBoundIndex = args.boundIndex;
                // column's data field.
                var columnDataField = args.dataField;
                // column's display field.
                var columnDisplayField = args.displayField;
                // cell's value.
                var value = args.value;
                if (columnDataField == "ShippedDate")
                    value = dataAdapter.formatDate(value, 'dd/MM/yyyy');

                $("#log").html("<br/>cellEndEdit - Row: " + rowIndex + ", Column: " + columnDataField + ", Value: " + value +"<br/>" + $("#log").html());
            });
        });
    </script>
</head>
<body class='default'>
    <div id="table"></div>
       <div style="font-size: 13px; font-family: Verdana; width: 800px; margin-top: 20px;">
        <h4>Event Log</h4>
        <div style="max-height: 300px; overflow: auto;" id="log"></div>
    </div>
</body>
</html>
